import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/config/const.dart';
import 'package:inmotion_pro/pages/mixin/cache_mixin.dart';
import 'package:inmotion_pro/pages/models/user_model.dart';
import 'package:inmotion_pro/pages/models/vehicle_model.dart';

// 游客服务
class GuestService extends GetxService with CacheMixin {
  // 全局服务
  static GuestService get to => Get.find();

  // 进入游客模式
  Future<void> enterGuestMode() async {
    await cacheMixinsetAllUsersUnselected();
    // 新增游客用户，只有赋值uid后，才能写入游客模式下的车辆数据
    await cacheMixinAddUser(
      UserModel.fromMap({})
        ..uid = guestUid
        ..isSelected = true,
    );
    // 设置游客模式数据
    await cacheMixinAuthorizedVehicleWriteToCache(_guestVehicleList);
    debugPrint('✅ 进入游客模式');
  }

  // 退出游客模式
  Future<void> leaveGuestMode() async {
    await cacheMixinIsUserExist(guestUid).then((isExist) async {
      // 重置云盒和HMI状态
      await cacheMixinResetTboxAndHmicStatus();
      if (isExist) {
        debugPrint('✅ 退出游客模式');
        await cacheMixinDeleteUser(guestUid);
      } else {
        // 刷新缓存的用户信息
        await cacheMixinRefreshCacheUserInfo();
      }
    });
  }

  // 游客模式的车辆模型
  final List<VehicleModel> _guestVehicleList = [
    VehicleModel.fromJson({
      "product_nickname": 'V12s-00KN8901',
      "ble_name": 'V12s-00KN8901',
      "sn": 'SDKKK456',
      "product_model": 'inmotion123'
    })
      ..isSelected = true,
    VehicleModel.fromJson({
      "product_nickname": 'V9-00KN8902',
      "ble_name": 'V9-00KN8902',
      "sn": 'SDKKK123',
      "product_model": 'inmotion456'
    }),
    VehicleModel.fromJson({
      "product_nickname": 'Rs_Jet-00KN8903',
      "ble_name": 'Rs_Jet-00KN8903',
      "sn": 'SDKKK789',
      "product_model": 'inmotion789'
    }),
  ];
}
